package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkInviteCodeSendLog;
import com.zb.mapper.ZjNetworkInviteCodeSendLogMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkInviteCodeSendLogService;
import com.zb.utils.TransactionUtil;
import com.zb.vo.NetworkInviteCodeSendLogListVo;
import com.zb.vo.NetworkInviteCodeSendLogVo;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点邀请码发送记录 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-03-26
 */
@Service
public class ZjNetworkInviteCodeSendLogServiceImpl extends ServiceImpl<ZjNetworkInviteCodeSendLogMapper, ZjNetworkInviteCodeSendLog> implements ZjNetworkInviteCodeSendLogService {
    @Resource
    private PlatformTransactionManager platformTransactionManager;
    @Resource(name = "asyncServiceExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    // 保存邀请码发送成功记录
    @Override
    public void saveSuccess(List<NetworkInviteCodeSendLogVo> inviteCodeSendLogVos) {
        if (CollectionUtil.isEmpty(inviteCodeSendLogVos)) {
            return;
        }
        this.save(true, inviteCodeSendLogVos);
    }

    // 保存邀请码发送失败记录
    @Override
    public void saveFailt(List<NetworkInviteCodeSendLogVo> inviteCodeSendLogVos) {
        if (CollectionUtil.isEmpty(inviteCodeSendLogVos)) {
            return;
        }
        this.save(false, inviteCodeSendLogVos);
    }

    /**
     * 保存邀请码发送记录
     * @param success
     * @param inviteCodeSendLogVos
     */
    private void save(boolean success, List<NetworkInviteCodeSendLogVo> inviteCodeSendLogVos) {
        threadPoolTaskExecutor.execute(() -> {
            TransactionStatus status = platformTransactionManager.getTransaction(TransactionUtil.transactionDefinition());
            try {
                List<ZjNetworkInviteCodeSendLog> codeSendLogs = new ArrayList<>();
                ZjNetworkInviteCodeSendLog codeSendLog = null;
                for (NetworkInviteCodeSendLogVo logVo : inviteCodeSendLogVos) {
                    codeSendLog = new ZjNetworkInviteCodeSendLog();
                    BeanUtil.copyProperties(logVo, codeSendLog);
                    codeSendLog.setSuccess(success);
                    codeSendLogs.add(codeSendLog);
                }
                this.saveBatch(codeSendLogs);
                platformTransactionManager.commit(status);
            } catch (Exception e) {
                platformTransactionManager.rollback(status);
                e.printStackTrace();
            }
        });
    }


    // 获取发送邀请码记录列表
    @Override
    public Map<String, Object> getNetworkInviteCodeList(Long custAccId, PageUtil pageUtil) {
        PageUtil.setPageLimit(pageUtil);
        QueryWrapper<ZjNetworkInviteCodeSendLog> wrapper = new QueryWrapper<>();
        wrapper.select("id", "send_username", "receive_phone", "content", "error", "is_success AS success", "gmt_create")
                .eq("cust_acc_id", custAccId)
                .orderByDesc("gmt_create");
        Page<ZjNetworkInviteCodeSendLog> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords().stream().map(log -> {
            NetworkInviteCodeSendLogListVo vo = new NetworkInviteCodeSendLogListVo();
            BeanUtil.copyProperties(log, vo);
            vo.setSendTime(log.getGmtCreate());
            return vo;
        }).toList());
    }
}
